//
//  KuKu_Image.m
//  kuku
//
//  Created by 清风徐来 on 2024/1/23.
//

import UIKit

@objc public class KuKu_Image: NSObject {
    @objc public static func stringToImage(_ str: String) -> UIImage? {
        var base64String = str

        // 检查并移除前缀
        if base64String.contains("data:image/png;base64,") {
            base64String = base64String.replacingOccurrences(of: "data:image/png;base64,", with: "")
        }
        if base64String.contains("data:image/jpeg;base64,") {
            base64String = base64String.replacingOccurrences(of: "data:image/jpeg;base64,", with: "")
        }

        // 将 Base64 字符串解码为 NSData
        guard let imageData = Data(base64Encoded: base64String, options: .init(rawValue: 0)) else {
            print("Unable to convert base64 string to NSData")
            return nil
        }

        // 使用 NSData 创建 UIImage
        if let photo = UIImage(data: imageData) {
            print("Image conversion successful")
            return photo
        } else {
            print("Unable to convert imageData to UIImage")
            return nil
        }
    }
}

